package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.ram.model.v20150501.UpdateAccessKeyRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.heima.behavior.dto.ArticleDto;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApArticle;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.feign.ApArticleController;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-18
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {

    @Autowired
    IApBehaviorEntryService entryService;

    @Autowired
    private ApArticleController apArticleFeign;

    @Override
    public ResponseResult getLike(LikesBehaviorDto dto) {

        if (dto == null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        User user = AppThreadLocalUtil.get();

        Integer userId = user.getUserId();

        ApLikesBehavior apLikesBehavior = new ApLikesBehavior();

        if (userId != 0) {

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, userId.toString());

            query.eq(ApBehaviorEntry::getType, 1);

            ApBehaviorEntry one = entryService.getOne(query);

            if (one == null) {

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(1);

                one.setEntryId(userId.toString());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        } else {
            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId, dto.getEquipmentId());

            //   String equipmentId = com.heima.search.dto.getEquipmentId();

            query.eq(ApBehaviorEntry::getType, 0);

            ApBehaviorEntry one = entryService.getOne(query);

            if (one == null) {

                one = new ApBehaviorEntry();

                one.setCreatedTime(new Date());

                one.setType(0);

                one.setEntryId(dto.getEquipmentId());

                entryService.save(one);
            }
            apLikesBehavior.setEntryId(one.getId());
        }
        apLikesBehavior.setCreatedTime(new Date());

        apLikesBehavior.setOperation(dto.getOperation());

        apLikesBehavior.setType(dto.getType());

        apLikesBehavior.setArticleId(dto.getArticleId());

        this.save(apLikesBehavior);

        ApArticle apArticle = apArticleFeign.selectArticleById(dto.getArticleId());

        int likes = apArticle.getLikes();

        Integer i = ++likes;

        apArticle.setLikes(i);

        /*int views = apArticle.getViews();

        Integer integer1 = ++views;

        apArticle.setViews(integer1);*/

        ResponseResult responseResult = apArticleFeign.selectArticleById(apArticle);

        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {

            return ResponseResult.okResult(apLikesBehavior.getId());
        }
        return null;
        /*User user = AppThreadLocalUtil.get();

        Integer id = null;

        if(user.getUserId()!=0){

            LambdaQueryWrapper<ApBehaviorEntry> query = new LambdaQueryWrapper<>();

            query.eq(ApBehaviorEntry::getEntryId,user.getUserId()).eq(ApBehaviorEntry::getType,1);

            ApBehaviorEntry one = entryService.getOne(query);

            if(one==null){

                getResponseResult(com.heima.search.dto);

                *//*LambdaQueryWrapper<ApBehaviorEntry> query1 = new LambdaQueryWrapper<>();

                query.eq(ApBehaviorEntry::getEntryId,com.heima.search.dto.getEquipmentId()).eq(ApBehaviorEntry::getType,0);

                ApBehaviorEntry one1 = entryService.getOne(query);

                if(one1==null){

                    getResponseResult(com.heima.search.dto);
                }else {
                    id = one1.getId();
                }*//*
            }else {

                updateResponseResult(com.heima.search.dto);
            }
        }else {


        }




        LambdaQueryWrapper<ApLikesBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApLikesBehavior::getArticleId,com.heima.search.dto.getArticleId()).eq(ApLikesBehavior::getType,com.heima.search.dto.getType()).eq(ApLikesBehavior::getEntryId,)



        return getResponseResult(com.heima.search.dto);*/
    }
}
